import request from '@/utils/request'
import Vue from 'vue'
const root = Vue.prototype

export function type(o) {
  return Object.prototype.toString
    .call(o)
    .match(/\[object (.*?)\]/)[1]
    .toLowerCase()
}

export const formatterKeys = {
  DIALOG_IN: 'dialog-in',
  DIALOG_OUT: 'dialog-out',
  SEARCH: 'search'
}

export const buttonKeys = {
  VIEW: 'view',
  ADD: 'add',
  EDIT: 'edit',
  DEL: 'del',
  EXPORT_EXCEL: 'exportExcel'
}

export function getBtnDefaultSetting(key) {
  const defaultProps = {
    size: 'small'
  }
  let defaultText = ''
  switch (key) {
    case buttonKeys.VIEW:
      defaultProps.icon = 'el-icon-view'
      defaultProps.type = 'success'
      defaultProps.circle = true
      defaultProps.type="text"
      defaultText = '查看'
      break
    case buttonKeys.ADD:
      defaultProps.icon = 'el-icon-plus'
      defaultProps.type = 'success'
      defaultText = '新增'
      break
    case buttonKeys.EDIT:
      defaultProps.icon = 'el-icon-edit'
      defaultProps.type = 'warning'
      defaultProps.circle = true
      defaultText = '修改'
      defaultProps.type="text"
      break
    case buttonKeys.DEL:
      defaultProps.icon = 'el-icon-delete'
      defaultProps.type = 'danger'
      defaultProps.circle = true
      defaultText = '删除'
      defaultProps.type="text"
      break
    case buttonKeys.EXPORT_EXCEL:
      defaultProps.icon = 'el-icon-printer'
      defaultProps.type = 'warning'
      defaultText = '导出'
      break
  }
  return { defaultProps, defaultText }
}

export function formatData(raw, formatter, delOldPro = false) {
  const newData = { ...raw }
  if (!formatter) return newData
  if (type(formatter) === 'object') {
    for (const prop in formatter) {
      if (newData[prop]) {
        Object.assign(newData, formatter[prop](newData[prop]))
        if (delOldPro) delete newData[prop]
      }
    }
  }
  if (type(formatter) === 'function') {
    formatter(newData)
  }
  return newData
}

/**
 * 合成v-model
 * @param nodes 节点
 * @param obj 响应式对象
 * @returns {null|*}
 */
export function mergeVModel(nodes, obj) {
  if (!nodes) return null
  return nodes.map(v => {
    if (!v.componentOptions) {
      v.children = mergeVModel.call(this, v.children, obj)
    } else if (v.componentOptions.tag !== 'el-form-item' && v.componentOptions.children) {
      v.componentOptions.children = mergeVModel.call(this, v.componentOptions.children, obj)
    } else {
      if (v.data.attrs.skip !== undefined) return v
      const modelKey = v.componentOptions.propsData.prop
      if (modelKey === undefined) throw '必须在el-form-item 指定 prop'
      v.componentOptions.children.map(el => {
        if (!el) return null
        const { propsData } = el.componentOptions
        if (propsData === undefined) {
          el.componentOptions.propsData = {}
        } else {
          const defaultValue = propsData.value
          if (defaultValue && !obj[modelKey]) {
            obj[modelKey] = defaultValue
          }
        }
        el.componentOptions.propsData.value = obj[modelKey]
        if (el.componentOptions.listeners === undefined) {
          el.componentOptions.listeners = {}
        }
        el.componentOptions.listeners.input = e => {
          Vue.set(obj, modelKey, e)
        }
        return el
      })
    }
    return v
  })
}

/**
 * 提示框
 * @param type
 * @param title 提示标题
 * @param successTip 操作成功后提示的文字
 * @param loadingText
 * @param reqEntity 请求体
 * @returns {Promise<void>}
 */
export function func_ask_for({ type = 'warning', title = '此操作将永久删除该记录, 是否继续?', loadingText = '正在删除', successTip = '删除成功', ...reqEntity }) {
  return new Promise(resolve => {
    root
      .$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type,
        beforeClose: async(action, instance, done) => {
          if (action === 'cancel') {
            done()
          } else {
            try {
              instance.confirmButtonLoading = true
              instance.confirmButtonText = loadingText
              // return console.log(reqEntity)
              const res = await request(reqEntity)
              root.$message.success(successTip || res.msg)
              done()
            } catch (e) {
              instance.confirmButtonText = '重试'
              console.log(e)
            } finally {
              instance.confirmButtonLoading = false
            }
          }
        }
      })
      .then(resolve)
      .catch(e => e)
  })
}